package org.pz.mq.demo;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MqDemoConfig {

    /**
     * 直连交换机，
     * 交换机通过 routeKey 匹配队列，给每一个队列分配消息
     * */
    public static final String direct_queue1 = "direct_queue1";
    public static final String direct_queue2 = "direct_queue2";
    public static final String direct_queue3 = "direct_queue3";
    public static final String direct_exchange = "direct_exchange";
    public static final String direct_routeKey = "direct_routeKey";
    public static final String direct_routeKey1 = "direct_routeKey1";

    @Bean
    public Queue directQueue1() {
        return new Queue(direct_queue1);
    }
    @Bean
    public Queue directQueue2() {
        return new Queue(direct_queue2);
    }
    @Bean
    public Queue directQueue3() {
        return new Queue(direct_queue3);
    }
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(direct_exchange);
    }
    @Bean
    public Binding directBinding1() {
        return BindingBuilder.bind(directQueue1()).to(directExchange()).with(direct_routeKey);
    }
    @Bean
    public Binding directBinding2() {
        return BindingBuilder.bind(directQueue2()).to(directExchange()).with(direct_routeKey);
    }
    @Bean
    public Binding directBinding3() {
        return BindingBuilder.bind(directQueue3()).to(directExchange()).with(direct_routeKey1);
    }

    /**
     * 扇形交换机
     * 绑定三个队列，每个队列都能收到消息
     * */
    public static final String fanout_queue1 = "fanout_queue1";
    public static final String fanout_queue2 = "fanout_queue2";
    public static final String fanout_queue3 = "fanout_queue3";
    public static final String fanout_exchange = "fanout_exchange";
    @Bean
    public Queue fanoutQueue1() {
        return new Queue(fanout_queue1);
    }
    @Bean
    public Queue fanoutQueue2() {
        return new Queue(fanout_queue2);
    }
    @Bean
    public Queue fanoutQueue3() {
        return new Queue(fanout_queue3);
    }
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(fanout_exchange);
    }
    @Bean
    public Binding fanoutBinding1() {
        return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
    }
    @Bean
    public Binding fanoutBinding2() {
        return BindingBuilder.bind(fanoutQueue2()).to(fanoutExchange());
    }
    @Bean
    public Binding fanoutBinding3() {
        return BindingBuilder.bind(fanoutQueue3()).to(fanoutExchange());
    }

    /**
     * 重试机制
     * */
    public static final String retry_queue = "retry_queue";
    public static final String retry_failure_queue = "retry_failure_queue";
    public static final String retry_exchange = "retry_exchange";
    public static final String retry_routeKey = "retry_routeKey";
    public static final String retry_failure_routeKey = "retry_failure_routeKey";
    @Bean
    public Queue retryQueue() {
        return new Queue(retry_queue);
    }
    @Bean
    public Queue retryFailureQueue() {
        return new Queue(retry_failure_queue);
    }
    @Bean
    public DirectExchange retryExchange() {
        return new DirectExchange(retry_exchange);
    }
    @Bean
    public Binding retryBinding1() {
        return BindingBuilder.bind(retryQueue()).to(retryExchange()).with(retry_routeKey);
    }
    @Bean
    public Binding retryFailureBinding1() {
        return BindingBuilder.bind(retryFailureQueue()).to(retryExchange()).with(retry_failure_routeKey);
    }

    /**
     * 死信队列
     * */
    public static final String demo_queue = "demo_queue";
    public static final String dead_queue = "dead_queue";
    public static final String demo_exchange = "demo_exchange";
    public static final String dead_exchange = "dead_exchange";
    public static final String demo_routeKey = "demo_routeKey";
    public static final String dead_routeKey = "dead_routeKey";
    @Bean
    public Queue demoQueue() {
        return QueueBuilder.durable(demo_queue)
                .deadLetterExchange(dead_exchange)
                .deadLetterRoutingKey(dead_routeKey)
                .build();
    }
    @Bean
    public Queue deadQueue() {
        return new Queue(dead_queue);
    }
    @Bean
    public DirectExchange demoExchange() {
        return new DirectExchange(demo_exchange);
    }
    @Bean
    public DirectExchange deadExchange() {
        return new DirectExchange(dead_exchange);
    }
    @Bean
    public Binding demoBinding() {
        return BindingBuilder.bind(demoQueue()).to(demoExchange()).with(demo_routeKey);
    }
    @Bean
    public Binding deadBinding() {
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with(dead_routeKey);
    }
}
